﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.QingJia;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;
using System.Collections.Generic;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.QingJia
{
    public class QingJiaGetCommandHandler:IRequestHandler<QingJiaGetCommand,APIPanging<QingJiaDto>>
    {
        private readonly IBaseRepository<LeaveApplicationForm> lRepo;
        private readonly IMapper mapper;
        private readonly IBaseRepository<User> uRepo;

        public QingJiaGetCommandHandler(IBaseRepository<LeaveApplicationForm>lRepo,IMapper mapper,IBaseRepository<User>uRepo)
        {
            this.lRepo = lRepo;
            this.mapper = mapper;
            this.uRepo = uRepo;
        }



        public async Task<APIPanging<QingJiaDto>> Handle(QingJiaGetCommand request, CancellationToken cancellationToken)
        {
            APIPanging<QingJiaDto> result = new APIPanging<QingJiaDto>();
            result.Code = APIEnum.查询成功;
            result.Msg = "查询成功";
            //请假列表全部信息
            var leave = lRepo .GetAll().Result;          
            //查询当前登录人是否是请假申请的申请人
            var m = leave.Where(x => x.UserName == request.Name);
            //查询登录人是否是领导
            var u = uRepo.GetAll().Result.Where(x => x.DepId == request.DepartId && x.HaoId == 1);
            //查询当前登录人是否是总负责人
            var z = uRepo.GetAll().Result.Where(x => x.UserId == request.UserId && x.HaoId == 2);
            if (u.Count() > 0)
            {
                var le = leave.Where(x => x.DepartId == request.DepartId);
                if (!string.IsNullOrEmpty(request.UserName))
                {
                    le = le.Where(x => x.UserName.Contains(request.UserName));
                }

                if (request.BeginTime != null && request.EndTime != null)
                {
                    le = le.Where(x => x.BeginTime >= request.BeginTime.Value && x.EndTime <= request.EndTime.Value);
                }
                if (request.Status != null)
                {
                    le = le.Where(x => x.Status == request.Status);
                }

                int TotalCount = le.Count();
                int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
                le = le.OrderByDescending(x => x.FormId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
                result.TotalCount = TotalCount;
                result.TotalPage = TotalPage;
                result.List = mapper.Map<List<QingJiaDto>>(le.ToList());
                return await  Task.FromResult(result);
            }
            else
            {

                if (!string.IsNullOrEmpty(request.UserName))
                {
                    m = m.Where(x => x.UserName.Contains(request.UserName));
                }

                if (request.BeginTime != null && request.EndTime != null)
                {
                    m = m.Where(x => x.BeginTime >= request.BeginTime.Value && x.EndTime <= request.EndTime.Value);
                }
                if (request.Status != null)
                {
                    m = m.Where(x => x.Status == request.Status);
                }
                int TotalCount = m.Count();
                int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
                m = m.OrderByDescending(x => x.FormId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
                result.TotalCount = TotalCount;
                result.TotalPage = TotalPage;
                result.List = mapper.Map<List<QingJiaDto>>(m.ToList());
               
            }

            if (z.Count() > 0)
            {
                 if (!string.IsNullOrEmpty(request.UserName))
                {
                    leave = leave.Where(x => x.UserName.Contains(request.UserName));
                }

                if (request.BeginTime != null && request.EndTime != null)
                {
                    leave = leave.Where(x => x.BeginTime >= request.BeginTime.Value && x.EndTime <= request.EndTime.Value);
                }
                if (request.Status != null)
                {
                    leave = leave.Where(x => x.Status == request.Status);
                }
                int TotalCount = m.Count();
                int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
                leave = leave.OrderByDescending(x => x.FormId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
                result.TotalCount = TotalCount;
                result.TotalPage = TotalPage;
                result.List = mapper.Map<List<QingJiaDto>>(leave.ToList());

            }




            return await Task.FromResult(result);

        }
    }
}


    

